package com.dlwx.firefighting.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dlwx.firefighting.R;
import com.dlwx.firefighting.bean.CartBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.inter.CartInter;
import com.dlwx.firefighting.utils.DialogUtils;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;


/**
 * Created by Administrator on 2017/6/16.
 */

public class CartAdapter extends ListBaseAdapter<CartBean> {

    private Context context;
    private  CartInter cartInter;
    private boolean isChoose=false;
    private String userId;

    public CartAdapter(Context context, CartInter cartInter) {
        super(context);
        this.context=context;
        this.cartInter=cartInter;
        userId= (String) SPUtils.get(context,Constant.USER_ID,"");
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_cart;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, final int position) {
//
        final ImageView titleSelectImg=holder.getView(R.id.title_select_img);
        final ImageView contentSelectImg=holder.getView(R.id.content_select_img);
        final ImageView addImg=holder.getView(R.id.add_img);
        final ImageView subtractImg=holder.getView(R.id.subtract_img);
        final ImageView imageView=holder.getView(R.id.image);
         final TextView numTv=holder.getView(R.id.num_tv);
        final TextView deleteTv=holder.getView(R.id.delete_tv);
        final TextView priceTv=holder.getView(R.id.price_tv);
         TextView shopNameTv=holder.getView(R.id.shop_name_tv);
         TextView goodsNameTv=holder.getView(R.id.goods_name_tv);
         TextView goodsDescTv=holder.getView(R.id.goods_desc_tv);
        final LinearLayout titleLl=holder.getView(R.id.title_ll);
//
        final CartBean bean = getDataList().get(position);
        final String goodsNum=bean.getGoods_number();
        numTv.setText(goodsNum);
        Glide.with(context).load(bean.getGoods_thumb()).error(R.mipmap.error_small).into(imageView);
        priceTv.setText(context.getString(R.string.rmb)+bean.getSubtotal());
        goodsNameTv.setText(bean.getGoods_name());
       if(bean.isChoose()) {
           titleSelectImg.setImageResource(R.mipmap.select_true);
           contentSelectImg.setImageResource(R.mipmap.select_true);
       }else {
           titleSelectImg.setImageResource(R.mipmap.select);
           contentSelectImg.setImageResource(R.mipmap.select);
       }
        addImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num=Integer.parseInt(goodsNum);
                num++;
                updateCartNum(bean,num,numTv,priceTv,position);
            }
        });
        subtractImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num=Integer.parseInt(goodsNum);
                if(num<2){
                    return;
                }
                num--;
                updateCartNum(bean,num,numTv,priceTv,position);
            }
        });
        contentSelectImg.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if(bean.isChoose()){
                   cartInter.removeCart(bean,position);
               }else{
                   cartInter.addCart(bean,position);
               }
           }
       });
        titleLl.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if(bean.isChoose()){
                   cartInter.removeCart(bean,position);
               }else{
                   cartInter.addCart(bean,position);
               }
           }
       });
        deleteTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cartInter.deleteCart(bean,position);
            }
        });
    }

    public void updateCartNum(final CartBean cartBean, int num, final TextView numTv, final TextView priceTv, final int position){
        DialogUtils.showProgressDlg(context,"");
        OkGo.<String>post(Constant.UPDATE_CARTNUM)
                .tag(this)
                .params("userid", userId)
                .params("rec_id", cartBean.getRec_id())
                .params("number", num)
                .params("goods_id", cartBean.getGoods_id())
                .params("suppid", "")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
//                        DealResult dealResult = new DealResult();
//                        StatusBean bean = dealResult.dealOne(context, response,new TypeToken<BaseOneBean<StatusBean>>(){}.getType());
//                        if(bean==null){return;}
//                        numTv.setText(""+bean.getNumber());
//                        priceTv.setText(bean.getSubtotal());
////                        ((CartFragment)context).refresh1(position,bean.getNumber(),bean.getSubtotal());
//                        cartInter.addCart(cartBean,position);
                    }
                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(context, context.getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }
}
